import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:get/get_navigation/src/extension_navigation.dart';

import '../util.dart';

/**
 * @Description: 首页标题栏
 * @Author: 下雨了
 * @CreateDate: 2022-04-01 9:42
 * @UpdateUser: 更新者
 * @UpdateDate:  2022-04-01 9:42
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */

PreferredSizeWidget HomeAppBar(
  BuildContext context, {
  double elevation = 0, // 默认高度
  Color? backgroundColor = ColorsUtil.green, // 默认背景颜色
  IconData? iconData, // 标题图标
  String? title, // 标题
  Color? titleColor, // 标题颜色
  Widget? leading, // 左侧返回
  Color? leadingColor = Colors.white, // 左侧按钮颜色
  VoidCallback? backTap, // 左侧还回按钮监听
  List<Widget>? actions, // 右侧
  PreferredSizeWidget? bottom, // 底部
}) {
  return AppBar(
    elevation: elevation,
    centerTitle: true,
    backgroundColor: backgroundColor,
    title: CentreText(iconData, title, titleColor),
    leading: leading ??
        DefaultBack(context, leadingColor: leadingColor, callback: backTap),
    actions: actions,
    bottom: bottom,
  );
}

/// 中间文字
Widget CentreText(
  IconData? iconData, // 标题图标
  String? title,
  Color? titleColor, // 标题颜色
) {
  return iconData == null
      ? Text(
          title ?? "",
          style:
              pfMediumStyle(color: titleColor ?? Colors.white, fontSize: 18.sp),
        )
      : RichText(
          text: TextSpan(children: [
            WidgetSpan(
              child: Icon(
                iconData,
                color: Colors.white,
                size: 20,
              ),
            ),
            TextSpan(
              text: title ?? "",
              style: pfMediumStyle(
                  color: titleColor ?? Colors.white, fontSize: 18.sp),
            ),
          ]),
        );
}

/// 默认返回按钮
Widget DefaultBack(
  BuildContext context, {
  Color? leadingColor = Colors.white70,
  VoidCallback? callback,
}) {
  return IconButton(
    onPressed: () {
      if (callback != null) {
        callback();
      }
      Get.back();
    },
    icon: Icon(
      Icons.arrow_back_ios_new_outlined,
      color: leadingColor,
    ),
  );
}
